package common.configuration;


import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;

import se.sics.kompics.address.Address;
import se.sics.kompics.network.NetworkConfiguration;
import se.sics.kompics.network.Transport;
import se.sics.kompics.p2p.bootstrap.BootstrapConfiguration;
import kompics.JettyWebServerConfiguration;

/**
 * The <code>Configuration</code> class.
 * 
 * @author Cosmin Arad <cosmin@sics.se>
 * @version $Id$
 */
public class ExecutionConfiguration {
	public InetAddress ip = null;
	{
		try {
			ip = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
		}
	}
	int networkPort = 8081;
	int webPort = 8080;
	int bootId = Integer.MAX_VALUE;
	int cyclonMonitorId = Integer.MAX_VALUE - 1;
	int controlId = Integer.MAX_VALUE - 2;

	Address bootServerAddress = new Address(ip, networkPort, bootId);
	Address cyclonMonitorServerAddress = new Address(ip, networkPort,
			cyclonMonitorId);
	Address controlServerAddress = new Address(ip, networkPort,
			controlId);
	Address peer0Address = new Address(ip, networkPort, 0);

	int webRequestTimeout = 10000;
	int webThreads = 2;
	String webAddress = "http://" + ip.getHostAddress() + ":" + webPort + "/";
	String homePage = "<h2>Welcome to the Kompics Peer-to-Peer Framework!</h2>"
			+ "<a href=\"" + webAddress + bootId + "/"
			+ "\">Bootstrap Server</a><br>" + "<a href=\"" + webAddress
			+ cyclonMonitorId + "/" + "\">Monitor Server</a><br>"
			+ "<a href=\"" + webAddress + controlId + "/"
			+ "\">Control Room</a><br>";

	JettyWebServerConfiguration webConfiguration = new JettyWebServerConfiguration(
			ip, webPort, webRequestTimeout, webThreads, homePage);

	BootstrapConfiguration bootConfiguration = new BootstrapConfiguration(
			bootServerAddress, 999999999, 4000, 10, 30000, webPort, webPort);

	MonitorConfiguration monitorConfiguration = new MonitorConfiguration(
			cyclonMonitorServerAddress, 10000, 5000, webPort, Transport.TCP);

	CyclonConfiguration cyclonConfiguration = new CyclonConfiguration(Global.sl, Global.vs,
			1000, 500000, new BigInteger("2").pow(20), 20);

	NetworkConfiguration networkConfiguration = new NetworkConfiguration(ip,
			networkPort, 0);
	
	TManConfiguration tmanConfiguration = new TManConfiguration(1000);
	
	AggregationConfiguration aggregationConfiguration = new AggregationConfiguration(1000);
	
	ControlConfiguration controlConfiguration = new ControlConfiguration(controlServerAddress);

	public void set() throws IOException {
		String c = File.createTempFile("jetty.web.", ".conf").getAbsolutePath();
		webConfiguration.store(c);
		System.setProperty("jetty.web.configuration", c);

		c = File.createTempFile("bootstrap.", ".conf").getAbsolutePath();
		bootConfiguration.store(c);
		System.setProperty("bootstrap.configuration", c);

		c = File.createTempFile("cyclon.monitor.", ".conf").getAbsolutePath();
		monitorConfiguration.store(c);
		System.setProperty("cyclon.monitor.configuration", c);

		c = File.createTempFile("cyclon.", ".conf").getAbsolutePath();
		cyclonConfiguration.store(c);
		System.setProperty("cyclon.configuration", c);

		c = File.createTempFile("network.", ".conf").getAbsolutePath();
		networkConfiguration.store(c);
		System.setProperty("network.configuration", c);
		
		c = File.createTempFile("tman.", ".conf").getAbsolutePath();
		tmanConfiguration.store(c);
		System.setProperty("tman.configuration", c);

		c = File.createTempFile("aggregation.", ".conf").getAbsolutePath();
		aggregationConfiguration.store(c);
		System.setProperty("aggregation.configuration", c);
		
		c = File.createTempFile("control.", ".conf").getAbsolutePath();
		controlConfiguration.store(c);
		System.setProperty("control.configuration", c);
	}
}
